package com.imay.live.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.imay.live.R;

/**
 * Created by lzx on 2017/08/03 0003.
 */

public class WebpView extends SimpleDraweeView {
    private float radius;

    public WebpView(Context context) {
        super(context);
        init();

    }

    public WebpView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WebpView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public WebpView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        radius = Resources.getSystem().getDisplayMetrics().density * 4;
    }

    public void setAntion(int visibility) {
        if (hasController()) {
            if (getController().getAnimatable() != null) {
                if (visibility == VISIBLE) {
                    if (!getController().getAnimatable().isRunning()) {
                        getController().getAnimatable().start();
                    }
                } else {
                    if (getController().getAnimatable().isRunning()) {
                        getController().getAnimatable().stop();
                    }
                }
            }
        }
    }


    public void setUri(String url, int w, int h) {
        setUri(url, true, w, h);
    }

    public void setUri(String url, boolean auto, int w, int h) {
        GenericDraweeHierarchy hierarchy = getHierarchy();
        if (hierarchy != null) {
            hierarchy.setPlaceholderImage(R.drawable.shape_placeholder_2);
            RoundingParams roundingParams = RoundingParams.fromCornersRadius(radius);
            roundingParams.setOverlayColor(Color.WHITE);
            hierarchy.setRoundingParams(roundingParams);
        }
        PipelineDraweeControllerBuilder pipelineDraweeControllerBuilder = Fresco.newDraweeControllerBuilder()
                .setUri(url);
        if (h > 0) {
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                    .setResizeOptions(new ResizeOptions(w, h)).build();
            pipelineDraweeControllerBuilder.setImageRequest(request);
        }
        DraweeController controller =
                pipelineDraweeControllerBuilder
                        .setOldController(getController())
                        .setAutoPlayAnimations(auto)
                        .build();
        setController(controller);
    }

    /**
     * @param uri
     * @param auto
     * @param w
     * @param h
     */
    public void setUri(Uri uri, boolean auto, int w, int h) {
        PipelineDraweeControllerBuilder pipelineDraweeControllerBuilder = Fresco.newDraweeControllerBuilder()
                .setUri(uri);
        if (h > 0) {
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(w, h)).build();
            pipelineDraweeControllerBuilder.setImageRequest(request);
        }
        DraweeController controller =
                pipelineDraweeControllerBuilder
                        .setControllerListener(new BaseControllerListener<ImageInfo>(){

                        })
                        .setOldController(getController())
                        .build();
        setController(controller);
    }
}
